package com.hd.crdao.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.MyAdapter;
import com.hd.crdao.adapter.MyListViewAdapter;
import com.hd.crdao.base.BaseActivity;
import com.hd.crdao.bean.ShopListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 *
 *@author Administrator
 *@date 2017/7/28 0028
 */

public class SearchShopActivity extends BaseActivity implements View.OnClickListener {

    public ImageButton mBack;
    public EditText mEtSearch;
    public RecyclerView mRecyclerView;
    public LinearLayoutManager mLinearLayoutManager;
    public MyAdapter mMyAdapter;
    public RefreshLayout mRefreshLayout;
    public ListView mListView;
    public MyListViewAdapter mMyListViewAdapter;
    public String mHome_cityName;
    public RelativeLayout mRlNone;
    public String mStoreName;
    public String areaName="";
    public Runnable mRunnable;
    public List<ShopListBean.ListAreaBean> mAll;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_shop;
    }

    @Override
    protected void initView() {
        mRlNone = (RelativeLayout) findViewById(R.id.rl_none);
        //初始化RecyclerView
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler);
        //创建默认的线性LayoutManager
        mLinearLayoutManager = new LinearLayoutManager(this);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecyclerView.setLayoutManager(mLinearLayoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        mRecyclerView.setHasFixedSize(true);
        //设置条目间距
        mRecyclerView.addItemDecoration(new MyItemDecoration(8));
        mRefreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        mListView = (ListView) findViewById(R.id.listview);
        mBack = (ImageButton) findViewById(R.id.ib_back);
        mEtSearch = (EditText) findViewById(R.id.searchEt);

        mAll = new ArrayList<>();
        ShopListBean.ListAreaBean one = new ShopListBean.ListAreaBean();
        ShopListBean.ListAreaBean two = new ShopListBean.ListAreaBean();
        one.setName("区域:");
        two.setName("不限");
        mAll.add(0,one);
        mAll.add(1,two);
        mMyAdapter = new MyAdapter(mAll);
    }

    @Override
    protected void initData() {
        mStoreName = getIntent().getStringExtra("storeName");
        mHome_cityName = SPUtils.getString("home_cityName","");
        mRefreshLayout.autoRefresh();
    }

    @Override
    protected void initListener() {
        mBack.setOnClickListener(this);
        mEtSearch.setOnClickListener(this);
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                mRunnable = new Runnable() {
                    @Override
                    public void run() {
                        loadData(mHome_cityName, areaName, mStoreName);
                    }
                };
                ((View)refreshlayout).postDelayed(mRunnable,1000);
            }
        });
        mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadmore(1000);
            }
        });
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mMyListViewAdapter!=null){
                    ShopListBean.StoreInfoListBean item = (ShopListBean.StoreInfoListBean) mMyListViewAdapter.getItem(position);
                    String storeId = item.getId();
                    Intent i=new Intent(UIUtils.getContext(),ShopIndexActivity.class);
                    i.putExtra("storeId",storeId);
                    startActivity(i);
                }
            }
        });

        mMyAdapter.setOnMyItemClickListener(new MyAdapter.OnMyItemClickListener() {
            @Override
            public void myClick(View v, int pos) {
                if(pos == 0){
                    return;
                }else {
                    areaName = mAll.get(pos).getName();
                    mRefreshLayout.autoRefresh();
                }
            }

            @Override
            public void mLongClick(View v, int pos) {

            }
        });
    }

    //点击事件的监听
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case  R.id.ib_back:
                finish();
                break;
            case  R.id.searchEt:
                finish();
                break;
        }
    }
    /**
     * 加载数据
     */
    private void loadData(String home_cityName, String area, String storeName) {
        OkHttpUtils.post().url(Constants.SHOP_LIST_URL).addParams("appFlag","true").addParams("pageNo","1").addParams("pageSize","")
                .addParams("cityName",home_cityName).addParams("areaName",areaName).addParams("storeName",storeName)
                    .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                mRefreshLayout.finishRefresh();
                MyToast.show("网络开了个小差...");
            }

            @Override
            public void onResponse(String response, int id) {
                mRefreshLayout.finishRefresh();
                ALog.d(response);
                Gson gson = new Gson();
                ShopListBean shopListBean = gson.fromJson(response, ShopListBean.class);
                String success = shopListBean.getSuccess();
                String message = shopListBean.getMessage();
                final List<ShopListBean.ListAreaBean> listArea = shopListBean.getListArea();  //地区列表
                List<ShopListBean.StoreInfoListBean> storeInfoList = shopListBean.getStoreInfoList();   //店铺列表
                if(TextUtils.equals("00",success)){
                        mAll.addAll(listArea);
                        mRecyclerView.setAdapter(mMyAdapter);

                    if(storeInfoList.size()==0){
                        mListView.setVisibility(View.GONE);
                        mRlNone.setVisibility(View.VISIBLE);
                    }else {
                        mListView.setVisibility(View.VISIBLE);
                        mRlNone.setVisibility(View.GONE);
                        mMyListViewAdapter = new MyListViewAdapter(storeInfoList);
                        mListView.setAdapter(mMyListViewAdapter);
                    }
                }else {
                    MyToast.show(message);
                }
            }
        });
    }

    //item设置间隔方法
    private class MyItemDecoration extends RecyclerView.ItemDecoration {
        private int space;
        public MyItemDecoration(int space) {
            this.space = space;
        }
        @Override
        public void getItemOffsets(Rect outRect, View view,
                                   RecyclerView parent, RecyclerView.State state) {
            outRect.left = space;
            outRect.right = space;
            outRect.bottom = space;
            outRect.top = space;
        }
    }
}
